﻿using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;
using System.Collections.Generic;
using System;
//退勤答题记录表
namespace IPR_WebApi.Controllers
{
    public class AttendanceReturnAnswerLogController : ApiController
    {
        /// <summary>
        /// 乘务员学习内容统计
        /// </summary>
        /// <param name="swhere"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/AttendanceReturnAnswerLog/AttendanceReturnAnswerLogSearch")]
        public IHttpActionResult AttendanceReturnAnswerLogSearch(string swhere, int page, int limit)
        {
            string UserID = "";
            string UserName = "";
            string StartDate = "";
            string EndDate = "";
            string searchLevel = "";
            if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                swhere = string.Empty;
                foreach (var item in jsonObject)
                {
                    if (item.Key == "UserID")
                    {
                        if (item.Value != "")
                            UserID = item.Value;
                    }
                    if (item.Key == "UserName")
                    {
                        if (item.Value != "")
                            UserName = item.Value;
                    }
                    if (item.Key == "StartDate")
                    {
                        if (item.Value != "")
                            StartDate = item.Value;
                    }
                    if (item.Key == "EndDate")
                    {
                        if (item.Value != "")
                            EndDate = item.Value;
                    }
                    if (item.Key == "searchLevel")
                    {
                        if (item.Value != "")
                            searchLevel = item.Value;
                    }
                }
            }

            ResultMsg resultMsg = new ResultMsg();
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            string strSql = "";
            string strSqlpage = "";
            string strSqlRowCount = " SELECT COUNT(*) AS RowNum FROM ";
            string strSqlData = " SELECT * FROM ";
            strSql = " ( "+
                " SELECT ROW_NUMBER() OVER(order by AnswerDateTime desc) as Row, T1.ARID, T1.TFID, T1.TFQID,T4.DepName,CONVERT(nvarchar(20),AnswerDateTime,120) as AnswerDateTime, T1.UserID, T1.UserName, T5.Question, T5.OptionA, " +
                " T5.OptionB, T5.OptionC, T5.OptionD, T5.Answer, AnswerResult, IsRight, ISNULL(GetValue, 0) AS GetValue "+
                " FROM AttendanceReturnAnswerLog T1 "+
                " left join UserInfo T3 on T1.UserID = T3.UserID "+
                " left join Dep T4 on T3.DeptID = T4.DepID "+
                " LEFT JOIN TrainQuestion T5 ON T1.TFQID = T5.TQID " +
                " WHERE 1 = 1 " +
                " AND CONVERT(nvarchar(11),AnswerDateTime,20) BETWEEN '" + StartDate + "' AND '" + EndDate + "'";
            if(UserID!="")
            {
                strSql += " AND T1.UserID = '"+UserID+"'";
            }
            if (UserName != "")
            {
                strSql += " AND T1.UserName LIKE '%"+UserName+"%'";
            }
            if(LoginUser.DepLevel==0)
            {
                //--统计所有车间人员学习内容
            }
            else if(LoginUser.DepLevel==1)
            {
                //统计所有车队人员学习内容
                strSql += " AND T4.DepPID="+LoginUser.DepID.ToString();
            }
            else if(LoginUser.DepLevel == 2)
            {
                //统计当前车队人员
                strSql += " AND T4.DepID=" + LoginUser.DepID.ToString();
            }
            strSql += " ) TT ";

            strSqlpage = strSqlData+ strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);
            strSqlRowCount += strSql;
            try
            {

                int rowcount = new IPR_StudyMeetingMrgSys.BLL.AttendanceReturnAnswerLog().AttendanceReturnAnswerLogSearchCount(strSqlRowCount); 
                bool blExist = false;
                if (rowcount == 0) blExist = false;
                else
                {
                    DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.AttendanceReturnAnswerLog().AttendanceReturnAnswerLogSearch(strSqlpage);
                    if (dsSearch != null)
                        if (dsSearch.Tables.Count > 0)
                            if (dsSearch.Tables[0].Rows.Count > 0)
                            {
                                //返回token信息
                                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                                resultMsg.Data = dsSearch.Tables[0];
                                resultMsg.Count = rowcount;
                                blExist = true;
                            }
                }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }



    }
}
